import cn.changein.framework.base.auth.jwt.JwtTokenUtil;
import io.jsonwebtoken.*;
import io.jsonwebtoken.impl.Base64Codec;
import io.jsonwebtoken.impl.JwtMap;
import org.apache.http.client.utils.DateUtils;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p> TODO(用一句话描述该文件做什么) </p>
 *

 * @version V1.0
 * @Title: JwtUtils
 * @Package PACKAGE_NAME
 * @date 2021/1/23 15:59
 * @Copyright: companyName
 */
public class JwtUtils {

    public static void main(String[] args) {
        /*String encodeKey = Base64Codec.BASE64.encode("wxp"); //加密
        JwtBuilder builder = Jwts.builder().setId("888")
                .setSubject("小白")
                .setIssuedAt(new Date())//用于设置签发时间
                .signWith(SignatureAlgorithm.HS256, encodeKey);//用于设置签名秘钥
        long now = System.currentTimeMillis();//当前时间
        long exp = now + 1000 * 60;//过期时间为1分钟
        builder.setExpiration(new Date(exp));//用于设置过期时间
        builder.setIssuedAt(new Date(now));
        String compact = builder.compact();
        System.out.println(builder.compact());
        parseJwt(compact);*/

    /*    String compactJws = createJwt();

        parseMyJwt(compactJws);*/

        str();
      /*  long l = TimeUnit.DAYS.toMillis(1);

        System.out.println("ii===" + l);
        JwtMap jwtMap = new JwtMap();
        String userId = "123";
        jwtMap.put("userId", "123");
        jwtMap.put("phone", "13143413928");
        long currentTimeMillis = System.currentTimeMillis();
        long exp = currentTimeMillis + 1000 * 60;//过期时间为1分钟
        //
        String token = JwtTokenUtil.generateToken(userId, new Date(exp), jwtMap);

        Claims claimFromToken = JwtTokenUtil.getClaimFromToken(token);
        Object userId1 = claimFromToken.get("userId");

        String str = JsonUtil.toJson(claimFromToken);
        Map<String, Object> map = JsonUtil.parseMap(str);
        System.out.println(map);

        String memberId = "8";
        long now = System.currentTimeMillis();
        exp = now + TimeUnit.DAYS.toMillis(1);
        token = JwtTokenUtil.generateToken(memberId + "", new Date(exp), new JwtMap());//参数可自己配置
        claimFromToken = JwtTokenUtil.getClaimFromToken(token);
        userId1 = claimFromToken.get("sub");
        String subject = claimFromToken.getSubject();
        Date expiration = claimFromToken.getExpiration();

        JwtPayLoad jwtPayLoad = JwtPayLoad.toBean(claimFromToken);
        System.out.println(jwtPayLoad);
//        claimFromToken = JwtTokenUtil.getClaimFromToken(token);
        System.out.println(userId1);
        System.out.println(token);*/
    }


    public static JwtMap createHeaderMap() {
        JwtMap jwtMap = new JwtMap();
        jwtMap.put("alg", SignatureAlgorithm.HS512.getValue()); //alg属性表示签名的算法（algorithm）
        jwtMap.put("typ", "JWT"); //typ属性表示这个令牌（token）的类型（type），JWT 令牌统一写为JWT。
        return jwtMap;
    }


    public static String parseMyJwt(String compactJws) {
        JwtParser parser = Jwts.parser();
        String wxp = Base64Codec.BASE64.encode("wxp");
        parser.setSigningKey(wxp);
        Jws<Claims> claimsJws = parser.parseClaimsJws(compactJws);
        Claims body = claimsJws.getBody();
        JwsHeader header = claimsJws.getHeader();
        System.out.println("body=" + body);
        System.out.println("header=" + header);
        return compactJws;
    }

    public static String parseJwt(String compactJws) {
        JwtParser parser = Jwts.parser();
        String encodeKey = Base64Codec.BASE64.encode("wxp"); //加密
        parser.setSigningKey(encodeKey);
        Jws<Claims> claimsJws = parser.parseClaimsJws(compactJws);
        Claims body = claimsJws.getBody();
        JwsHeader header = claimsJws.getHeader();
        System.out.println("body=" + body);
        System.out.println("header=" + header);
        return "";
    }


    public static String str() {
        long now = System.currentTimeMillis();
        long exp = now + TimeUnit.DAYS.toMillis(1);
        Long memberId = 8L;
        String token = JwtTokenUtil.generateToken(memberId + "", new Date(exp), new JwtMap());//参数可自己配置
        System.out.println("token=" + token);

        Claims claimFromToken = JwtTokenUtil.getClaimFromToken(token);
        Date expiration = claimFromToken.getExpiration();
        String subject = claimFromToken.getSubject();

        String strDate = DateUtils.formatDate(expiration, "yyyy-MM-dd HH:mm:ss");
        System.out.println("strDate=" + strDate);
        System.out.println("subject=" + subject);
        return token;
    }
}
